package csvs

import (
	"fmt"
	"MyWuXiaGame/server/src/utils"
	"strconv"
	"strings"
)

type ConfigJobAttributeIndex struct {
	Idx			int		`json:"Idx"`
	Id			int		`json:"Id"`
	Name		string	`json:"Name"`
	AttrType	int		`json:"AttrType"`
	Desc		string	`json:"Desc"`
	Probvalue	int		`json:"Probvalue"`
}

type ManageJobAttributeIndex struct {

}

type TJobAttrKeyValue_Integer struct {
	AttrIdx		int
	AttrValue	int
}

type TGrowthLineJobAttr struct {
	JobAttrKeyValueList []*TJobAttrKeyValue_Integer
}

var (
	ConfigJobAttributeIndexSlice []*ConfigJobAttributeIndex
	ConfigJobAttributeIndexTypeMap	map[int]*ConfigJobAttributeIndex
	ConfigJobAttributeIndexNameMap	map[string]*ConfigJobAttributeIndex
	manageJobAttributeIndex *ManageJobAttributeIndex
)

func init() {
	ConfigJobAttributeIndexSlice = make([]*ConfigJobAttributeIndex, 0)
	utils.GetCsvUtilMgr().LoadCsv("JobAttributeIndex", &ConfigJobAttributeIndexSlice)

	MakeConfigJobAttributeIndexMap()
	return
}

func MakeConfigJobAttributeIndexMap() {
	ConfigJobAttributeIndexTypeMap = make(map[int]*ConfigJobAttributeIndex)
	ConfigJobAttributeIndexNameMap = make(map[string]*ConfigJobAttributeIndex)
	for _, v := range ConfigJobAttributeIndexSlice {
		ConfigJobAttributeIndexTypeMap[v.AttrType] = v
		ConfigJobAttributeIndexNameMap[v.Name] = v
	}
}

func GetManageJobAttributeIndex() *ManageJobAttributeIndex {
	if manageJobAttributeIndex == nil {
		manageJobAttributeIndex = new(ManageJobAttributeIndex)
	}
	return manageJobAttributeIndex
}

func (self * ManageJobAttributeIndex) Fill_TGrowthLineJobAttr(JobAttr *TGrowthLineJobAttr, AbilityStr string) {
	srcStr := AbilityStr
	descStrSlice := strings.Split(srcStr, ";")

	for _, v := range descStrSlice {
		KeyValueStrSlice := strings.Split(v, "=")
		if len(KeyValueStrSlice) != 2 {
			fmt.Println("JobAttribute 配置格式错误")
		}

		AttrNameStr := KeyValueStrSlice[0]
		AttrValueStr := KeyValueStrSlice[1]

		JobAttrKeyValue := new(TJobAttrKeyValue_Integer)

		JobAttributeIndexConfig, ok := ConfigJobAttributeIndexNameMap[AttrNameStr]
		if ok {
			JobAttrKeyValue.AttrIdx = JobAttributeIndexConfig.AttrType
		}

		AttrValue, err := strconv.Atoi(AttrValueStr)
		if err == nil {
			JobAttrKeyValue.AttrValue = AttrValue
		}

		JobAttr.JobAttrKeyValueList = append(JobAttr.JobAttrKeyValueList, JobAttrKeyValue)
	}
}

func GetJobAttributeConfigByIndex(AttrType int) *ConfigJobAttributeIndex {
	return ConfigJobAttributeIndexTypeMap[AttrType]
}